﻿// ----------------------
// DOM manipulate
// ----------------------

getImageSrc = function getImageSrc(img)
{
    if (img.src)
        return https2http(img.src);
    var attr = img.getAttribute("data-ks-lazyload");
    if (!attr)
        return "http:" + attr;
    var attr = img.getAttribute("data-src");
    if (attr)
        return "http:" + attr;
    return "";
}



// ----------------------
// Helper functions
// ----------------------

fixPrice = function fixPrice(price)
{
    return /[0-9\.]+/.exec(price)[0];
}

reverseString = function reverseString(str)
{
    var s = "";
    for (var i = str.length - 1; i >= 0; i--)
    {
        s += str[i];
    }
    return s;
}

fixString = (function ()
{
    var noWarp = /(\r|\n)/g;
    noWarp.compile(noWarp);
    var noFrontSpace = / */;
    noFrontSpace.compile(noFrontSpace);
    return function fixString(str)
    {
        str = str.replace(noWarp, "");
        return reverseString(reverseString(str.replace(noFrontSpace, "")).replace(noFrontSpace, ""));
    }
})();

existStrings = function existStrings(str, keywords)
{
    for(var i=0; i < keywords.length; i++)
    {
        if (str.search(keywords[i])!= -1)
            return true;
    }
    return false;
}

createKeywordSelector = function createKeywordSelector(searchStr, front, end)
{
    return createSelectorByKeyrowd(stringToKeywords(searchStr), front, end);
}

function createSelectorByKeyrowd(keys, front, end, sep)
{
    typeof front === "undefined" && (front = ":contains(");
    typeof end === "undefined" && (end = ")");
    typeof sep === "undefined" && (sep = ',');
    return front + keys.join(end + sep + front) + end;
}

stringToKeywords = function stringToKeywords(str)
{
    var keys = str.split(" ");
    var finalKeys = [];
    for (var i in keys)
    {
        finalKeys = finalKeys.concat(toggleString(keys[i]));
    }
    return finalKeys;
}

function toggleString(str)
{
    var strs = [str];
    for (var i in str)
    {
        i = Number(i);
        var c = str[i];
        if (c <= 'Z' && c >= 'A' || c <= 'z' && c >= 'a')
        {
            strs = extendOneBit(strs, i);
        }
    }
    return strs;
}

function extendOneBit(strs, bit)
{
    var add = [];
    for (var i in strs)
    {
        add[i] = toggleStrCase(strs[i], bit);
    }
    return add.concat(strs);
}

function toggleStrCase(str, bit)
{
    return str.substr(0, bit) + toggleCase(str[bit]) + str.substr(bit + 1);
}

function toggleCase(c)
{
    return c.charCodeAt(0) < 97 ? c.toLowerCase() : c.toUpperCase();
}

https2http = function https2http(url)
{
    return url.replace('https://', function ()
    {
        return "http://";
    })
}


checkItem = function checkItem(_top, onOk, onSkip, onAbort)
{
    var _r = _top.getBoundingClientRect();
    var r = {
        left: _r.left,
        top: _r.top += window.scrollY,
        width: _r.width,
        height: _r.height
    }

    window.scrollTo(0, r.top + r.height / 2 - window.innerHeight / 2);
    var padding = 10
    var borderSize = 4
    var space = Math.floor((padding + borderSize) / 2);
    var x = r.left - space;
    var y = r.top - space;
    var w = r.width + space * 2;
    var h = r.height + space * 2

    var p = document.body;
    var a = p.appendChild(createFillDiv(x, y, borderSize, h));
    var b = p.appendChild(createFillDiv(x, y, w, borderSize));
    var c = p.appendChild(createFillDiv(x + w - borderSize, y, borderSize, h));
    var d = p.appendChild(createFillDiv(x, y + h - borderSize, w, borderSize));

    var b1 = createButton("确认", bindClear(onOk));
    var b2 = createButton("跳过", bindClear(onSkip));
    var b3 = createButton("终止", bindClear(onAbort));

    putItemsV(p, x, y + h, [b1, b2, b3]);
    function bindClear(func)
    {
        return function ()
        {
            clearDivs();
            func();
        }
    }
    function clearDivs()
    {
        a.remove();
        b.remove();
        c.remove();
        d.remove();
        b1.remove();
        b2.remove();
        b3.remove();
    }
}
function createFillDiv(x, y, w, h, colorParam)
{
    var colorstr = colorParam ? colorParam : "#FF0000";
    var div = document.createElement("div");
    div.setAttribute("style", "z-index:1000;position:absolute;left:" + x + "px;top:" + y + "px;width:" + w + "px;height:" + h + "px;"
    + "background-color:" + colorstr);
    return div;
}

function createDiv(x, y, w, h, borderParam)
{
    var borderstr = borderParam ? borderParam : "4px solid #FF0000";
    var div = document.createElement("div");
    div.setAttribute("style", "z-index:1000;position:absolute;left:" + x + "px;top:" + y + "px;width:" + w + "px;height:" + h + "px;"
    + "border: " + borderstr);
    return div;
}

function createButton(name, onClicked)
{
    var a = document.createElement("a");
    a.href = "javascript:void(0)";
    a.setAttribute("style", "display:inline-block;" +
	"border-radius:.80em;" +
	"background-color:#277ac1;" +
	"background-image:linear-gradient(#43a6e2,#277ac1);" +
	"border:0;" +
	"padding: 0.5em 1em 0.5em 1em");
    a.text = name;
    a.addEventListener("click", onClicked);
    return a;
}

function putItemsV(_parent, x, y, items)
{
    const padding = 5;
    var cur = { x: x, y: y };
    for (var i = 0; i < items.length; i++)
    {
        var t = items[i];
        t.setAttribute("style", t.getAttribute("style") + ";z-index:1000;position:absolute;left:" + cur.x + "px;top:" + cur.y + "px;");
        _parent.appendChild(t);
        cur.x += t.offsetWidth + padding;
    }
    return cur;
}